<?php

namespace app\admin\controller\vppz;

use app\common\controller\Backend;
//use think\addons\Service;
//use think\Exception;

use app\admin\model\vppz\App;


/**
 * 系统配置
 *
 * @icon fa fa-gears
 */
class HealthService extends Base
{
	protected $branch = 0;	// 0：插件市场版，1：自主授权版

    protected $model = null;

   protected $searchFields = ['id','name'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\vppz\HealthService;
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->modelValidate = true;
            $params = $this->request->post('row/a');
            if (empty($params)) {
                $this->error(__('Parameter %s can not be empty', ''));
            }
            $form = $this->preExcludeFields($params);
            $result = false;
            try {
                $Note = $this->model->where(['name'=>$form['name']])->find();
                if(empty($Note) === FALSE){
					$this->error('服务名称已存在，重试看看呢');
                }
                $Data = array(
                    'name'=>$form['name'],
                    'weigh'=>$form['weigh'],
                    'content'=>$form['content'],
                    'image'=>$form['image'],
                );
                $ret = $this->model->save($Data);
                if(!$ret){
					$this->error('服务器刚刚走神了，重试看看呢');
				}
                $this->success();
            } catch (ValidateException|PDOException|Exception $e) {
                $this->error($e->getMessage());
            }
        }
        return parent::add();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $this->modelValidate = true;
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $form = $this->preExcludeFields($params);
        $result = false;
        try {
            $Note = $this->model->where(['name'=>$form['name']])->find();
            if(empty($Note) === FALSE && $Note['id'] != $ids){
                $this->error('服务名称已存在，重试看看呢');
            }
            $Data = array(
                'name'=>$form['name'],
                'weigh'=>$form['weigh'],
                'content'=>$form['content'],
                'image'=>$form['image'],
            );
            $ret = $this->model->where(['id'=>$ids])->update($Data);
            if(!$ret){
                $this->error('服务器刚刚走神了，重试看看呢');
            }
            $this->success();
        } catch (ValidateException|PDOException|Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success();

    }

}
